# -*- coding:utf-8 -*-
"""
@project: AutoTesting_pulsar
@author: chenhh
@file: Extract_data.py
@ide: PyCharm
@time: 2021/2/22
封装Extract_data方法:data_id, data_msg, data_items, data_code, msg,
"""

import json

import pytest

from Common.Read_xls import Readxlsdata
from Common import Request, Log, Consts


class Extractdata(object):
	def __init__(self):
		pass


	def extractparam(self, param):
		try:
			pa = param.split('.')
			return pa
		except:
			Log.MyLog.info("提取路径错误：%s" % param )
			raise


	def public_access(self,response,extract_param):
		if isinstance(extract_param, str):
			extract_param = self.extractparam(extract_param)
		else:
			extract_param = extract_param
		try:
			for i in range (0 , len(extract_param)):
				print(i)
				if isinstance(response, dict):
					b = extract_param[i]
					print(b)
					c = response[b]
					response = c
				else:
					data = json.loads(response)
					b = extract_param[i]
					c = data[b]
					response = c

			return response

		except:
			print('提取msg失败,response = %s' % response)
			raise

if __name__ == '__main__':
	tt = {"data":{"id":"6764f865-65d2-11eb-88d1-c20b114729eb"},"msg":"ok","result":True}
	extract_param = 'monitor_id=data.id'
	pa = extract_param.split('=')
	name = pa[0]
	param = pa[1]
	#print(extract_param[0])
	res = Extractdata()
	res1 = res.public_access(tt,param)
	a = {name:res1}
	print('字典',a)
	b = Consts.API_EXTRACTPARAM.update(a)
	print('键值对：',Consts.API_EXTRACTPARAM)
	print('a = {},类型是{}'.format(res1, type(res1)))

